package in.abip.chisame;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * Simulation activity
 * 
 * @author Julot
 * @since Tuesday, May 10th, 2011
 */
public class SimulationActivity extends NetworkActivity {
	/**
	 * @var boolean indicate whether simulation should be stopped or not 
	 */
	private boolean _stopped = true;
	
	private ProgressDialog _stopProgress = null;
	
	/**
	 * Runnable to run simulation.
	 */
	private Runnable _simulating = new Runnable(){
		public void run() {
			simulate();
		}
	};
	
    private Runnable _dismissingStop = new Runnable() {
        public void run() {
            _stopProgress.dismiss();
    		View view = findViewById(R.id.start);
    		view.setEnabled(true);
        }
    };
    
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.simulation_activity);
		bindStart();
		bindStop();
	}
	
	private void bindStart() {
		View view = (View) findViewById(R.id.start);
		view.setOnClickListener(
			new OnClickListener() {
				public void onClick(View view) {
					startSimulation();
				}
			}
		);
	}
	
	private void bindStop() {
		View view = (View) findViewById(R.id.stop);
		view.setOnClickListener(
			new OnClickListener() {
				public void onClick(View view) {
					stopSimulation();
				}
			}
		);
	}
	
	/**
	 * Start simulation in another thread.
	 */
	private void startSimulation() {
		View view = findViewById(R.id.stop);
		view.setEnabled(true);
		view = findViewById(R.id.start);
		view.setEnabled(false);
		
		Toast.makeText(
			SimulationActivity.this, 
			"Simulation Started", 
			Toast.LENGTH_SHORT
		).show();
        Thread thread =  new Thread(null, _simulating, "Simulating");
        thread.start();
	}
	
	/**
	 * Stop simulation.
	 */
	private void stopSimulation() {
		View view = findViewById(R.id.stop);
		view.setEnabled(false);
		
        _stopProgress = new ProgressDialog(this);
        _stopProgress.setMessage("Stopping simulation.\nPlease wait...");
        _stopProgress.setIndeterminate(true);
        _stopProgress.setCancelable(false);
        _stopProgress.show();
        
		_stopped = true;
	}
	
	/**
	 * Simulation controller.
	 * @throws InterruptedException
	 */
	private void simulate() {
		String lampId = "0102";
		String dimmerId = "0108";
		String blindId = "0101";
		_stopped = false;
		
		try {
			// Set start condition
			turnOffLamp(lampId);
			turnOffLamp(dimmerId);
			closeBlind(blindId);
			while (!_stopped) {
				openBlind(blindId);
				turnOnDimmer(dimmerId);
				turnOffDimmer(dimmerId);
				closeBlind(blindId);
				turnOnLamp(lampId);
				openBlind(blindId);
				turnOffLamp(lampId);
				turnOnDimmer(dimmerId);
				turnOnLamp(lampId);
				closeBlind(blindId);
				turnOffDimmer(dimmerId);
				turnOffLamp(lampId);
			}
		} catch (Exception e1) {
			
		}
		runOnUiThread(_dismissingStop);
	}
	
	/**
	 * Pause execution.
	 * @param time in millisecond
	 */
	private void sleep(long time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
		}
	}
	
	/**
	 * Open window blind.
	 * @param id
	 * @throws Exception 
	 */
	private void openBlind(String id) throws Exception {
//		getChachamaru().attention("0101000");
		sleep(3000);
		if (_stopped) throw new Exception("Stopped");
	}
	
	/**
	 * Close window blind.
	 * @param id
	 * @throws Exception 
	 */
	private void closeBlind(String id) throws Exception {
//		getChachamaru().attention("0101100");
		sleep(3000);
		if (_stopped) throw new Exception("Stopped");
	}
	
	/**
	 * Turn on dimmer incrementally.
	 * @param id
	 * @throws Exception 
	 */
	private void turnOnDimmer(String id) throws Exception {
		String value = "";
		for (int i = 1; i < 100; i++) {
			value = Integer.toString(i + 1000).substring(1);
			tcpOutputs.add(id + value);
			startSend();
			sleep(100);
			if (_stopped) throw new Exception("Stopped");
		}
	}
	
	/**
	 * Turn off dimmer incrementally.
	 * @param id
	 * @throws Exception 
	 */
	private void turnOffDimmer(String id) throws Exception {
		String value = "";
		for (int i = 99; i > 0; i--) {
			value = Integer.toString(i + 1000).substring(1);
			tcpOutputs.add(id + value);
			startSend();
			sleep(100);
			if (_stopped) throw new Exception("Stopped");
		}
	}
	
	/**
	 * Turn on lamp.
	 * @param id
	 * @throws Exception 
	 */
	private void turnOnLamp(String id) throws Exception {
//		getChachamaru().attention(id + "100");
		if (_stopped) throw new Exception("Stopped");
	}
	
	/**
	 * Turn off lamp.
	 * @param id
	 * @throws Exception 
	 */
	private void turnOffLamp(String id) throws Exception {
//		getChachamaru().attention(id + "000");
		if (_stopped) throw new Exception("Stopped");
	}
}
